package com.lifp.thread.lock;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @Description 多线程相关8种锁
 * @Author lfp
 * @Date 2022/7/25
 * @Version 1.0
 */

//资源类
class Phone {
    public void getHello(){
        System.out.println("------ getHello");
    }

    @SneakyThrows
    public synchronized void sendSMS(){
        //停留4秒
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------ sendSMS");
    }

    public synchronized void sendEmail(){
        System.out.println("------ sendEmail");
    }
}



public class Lock_8 {
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        Phone phone2 = new Phone();

        new Thread(()->{
            phone.sendSMS();
        },"AA").start();

        Thread.sleep(100);

        new Thread(()->{
            //phone.sendEmail();

            //phone.getHello();
            phone2.sendEmail();
        },"BB").start();
    }
}
